#coding=utf8
'''
Created on 2016��6��11��
'''
import os
import json
import time
import sys
import copy
from DataReader import DataReader
from DataNode import DataNode
from DataNodeForOutlier import DataNodeForOutlier
from DataNodeForBusy import DataNodeForBusy
from DataProcess import  DataProcess
path = '../../data'
class DataProcessOutlierAndBusy:
     def __init__(self):
         self.data_reader = DataReader(path)
     def get_data(self):
        while True:
            temp=self.data_reader.get_one_set_data()
            if temp == None:
                break
            yield temp
     def getOutlierAndBusy(self,topNum):
         outlier={}
         busy={}
         data=[]
         for row in self.get_data():
             data.append(row)
         maxWrit=-1
         maxRead=-1
         maxSend=-1
         maxRecv=-1
         for it in data:
             for k,v in it.items():
                 if(v['disk_read_speed']>maxRead):
                     maxRead=v['disk_read_speed']
                 if(v['disk_write_speed']>maxWrit):
                     maxWrit=v['disk_write_speed']
                 if(v['net_send_speed']>maxSend):
                     maxSend=v['net_send_speed']
                 if(v['net_rec_speed']>maxRecv):
                     maxRecv=v['net_rec_speed']
         for k,v in data[0].items():
             outlier[k]=[]
             busy[k]=[]
         for it in data:
             for k,v in it.items():
                 outlier_node=DataNodeForOutlier(v['timestamp'],v['cpu_all_percent']/100.0,v['memory_percent']/100.0,v['disk_read_speed']/(1.0*maxRead),v['disk_write_speed']/(1.0*maxWrit),v['net_send_speed']/(1.0*maxSend),v['net_rec_speed']/(1.0*maxRecv))
                 outlier[k].append(outlier_node)
                 busy_node=DataNodeForBusy(v['timestamp'],v['cpu_all_percent']/100.0,v['memory_percent']/100.0,v['disk_read_speed']/(1.0*maxRead),v['disk_write_speed']/(1.0*maxWrit),v['net_send_speed']/(1.0*maxSend),v['net_rec_speed']/(1.0*maxRecv))
                 busy[k].append(busy_node)
         for k,v in outlier.items():
             v.sort()
         for k,v in busy.items():
             v.sort()
         re={}
         for k,v in busy.items():
             re[k]={}
             re[k]['outlier']=[]
             re[k]['busy']=[]
         count =0
         for k,v in busy.items():
             count=0
             for it in v:
                 temp={}
                 temp['timestamp']=it.timestamp
                 temp['cpu_all_percent']=it.cpu_all_percent
                 temp['memory_percent']=it.memory_percent
                 temp['disk_read_speed']=it.disk_read_speed
                 temp['disk_write_speed']=it.disk_write_speed
                 temp['net_send_speed']=it.net_send_speed
                 temp['net_rec_speed']=it.net_rec_speed
                 re[k]['busy'].append(temp)
                 count+=1
                 if count== topNum:
                     break
         count=0
         for k,v in outlier.items():
             count=0
             for it in v:
                 temp={}
                 temp['timestamp']=it.timestamp
                 temp['cpu_all_percent']=it.cpu_all_percent
                 temp['memory_percent']=it.memory_percent
                 temp['disk_read_speed']=it.disk_read_speed
                 temp['disk_write_speed']=it.disk_write_speed
                 temp['net_send_speed']=it.net_send_speed
                 temp['net_rec_speed']=it.net_rec_speed
                 re[k]['outlier'].append(temp)
                 count+=1
                 if count==topNum:
                     break
         return re
     def getOutlierAndBusy2(self,topNum,data):
         outlier={}
         busy={}
         maxWrit=-1
         maxRead=-1
         maxSend=-1
         maxRecv=-1
         for k,v in data.items():
             for k2,v2 in v.items():
                 for it in v2:
                     if k2=='disk_read_speed':
                         if it>maxRead:
                             maxRead=it
                     if k2=='disk_write_speed':
                         if it>maxWrit:
                             maxWrit=it
                     if k2=='net_send_speed':
                         if it>maxSend:
                             maxSend=it
                     if k2=='net_rec_speed':
                         if it>maxRecv:
                             maxRecv=it
         for k,v in data.items():
             outlier[k]=[]
             busy[k]=[]
         for k,v in data.items():
             for count in range(len(v['timestamp'])):
                 outlier_node=DataNodeForOutlier(v['timestamp'][count],v['cpu_all_percent'][count]/100.0,v['memory_percent'][count]/100.0,v['disk_read_speed'][count]/(1.0*maxRead),v['disk_write_speed'][count]/(1.0*maxWrit),v['net_send_speed'][count]/(1.0*maxSend),v['net_rec_speed'][count]/(1.0*maxRecv))
                 outlier[k].append(outlier_node)
                 busy_node=DataNodeForBusy(v['timestamp'][count],v['cpu_all_percent'][count]/100.0,v['memory_percent'][count]/100.0,v['disk_read_speed'][count]/(1.0*maxRead),v['disk_write_speed'][count]/(1.0*maxWrit),v['net_send_speed'][count]/(1.0*maxSend),v['net_rec_speed'][count]/(1.0*maxRecv))
                 busy[k].append(busy_node)
         for k,v in outlier.items():
             v.sort()
         for k,v in busy.items():
             v.sort()
         re={}
         for k,v in busy.items():
             re[k]={}
             re[k]['outlier']=[]
             re[k]['busy']=[]
         count =0
         for k,v in busy.items():
             count=0
             for it in v:
                 temp={}
                 temp['timestamp']=it.timestamp
                 temp['cpu_all_percent']=it.cpu_all_percent
                 temp['memory_percent']=it.memory_percent
                 temp['disk_read_speed']=it.disk_read_speed
                 temp['disk_write_speed']=it.disk_write_speed
                 temp['net_send_speed']=it.net_send_speed
                 temp['net_rec_speed']=it.net_rec_speed
                 re[k]['busy'].append(temp)
                 count+=1
                 if count== topNum:
                     break
         count=0
         for k,v in outlier.items():
             count=0
             for it in v:
                 temp={}
                 temp['timestamp']=it.timestamp
                 temp['cpu_all_percent']=it.cpu_all_percent
                 temp['memory_percent']=it.memory_percent
                 temp['disk_read_speed']=it.disk_read_speed
                 temp['disk_write_speed']=it.disk_write_speed
                 temp['net_send_speed']=it.net_send_speed
                 temp['net_rec_speed']=it.net_rec_speed
                 re[k]['outlier'].append(temp)
                 count+=1
                 if count==topNum:
                     break
         return re
if __name__ == '__main__':
     dr = DataProcessOutlierAndBusy()
     forData=DataProcess()
     temp=dr.getOutlierAndBusy2(4,forData.get_normal_data(5))
     print temp
     